﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace BaiduSpider.Helpers
{
    public class DownloadHelper
    {
        public static Stream DownloadFile(string url)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.KeepAlive = false;
            httpWebRequest.Referer = "";
            //httpWebRequest.Host = "baike.baidu.com";
            httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36";
            httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
            httpWebRequest.Timeout = 30 * 1000;
            httpWebRequest.Method = "GET";

            HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            if (webResponse.StatusCode != HttpStatusCode.OK)
            {
                return null;
            }
            return webResponse.GetResponseStream();
        }
        public static bool DownloadFile(string url, string fileName, string referer)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.KeepAlive = false;
            httpWebRequest.Host = url.Split("/")[2];
            httpWebRequest.Referer=referer;
            httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36";
            httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
            httpWebRequest.Timeout = 30 * 1000;
            httpWebRequest.Method = "GET";

            HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            if (webResponse.StatusCode != HttpStatusCode.OK)
            {
                return false;
            }
            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                webResponse.GetResponseStream().CopyTo(fs);
            }
            return true;
        }
    }
}
